<html>
  <head>
    <script type="application/javascript">
      /** @type {HTMLCanvasElement} */
      function draw() {
        var ctx = document.getElementById("canvas").getContext("2d");
        ctx.font = "48px serif";
        ctx.fillText("Hello world", 10, 50);
        ctx.strokeText("Hello world", 50, 100);
      }

      function draw2() {
        var ctx = document.getElementById("canvas").getContext("2d");
        var text = ctx.measureText("foo"); // TextMetrics object
        // alert(text.width);
        console.log(text.width);
      }
    </script>
  </head>
  <body onload="draw2();">
    <canvas id="canvas" width="300" height="300"></canvas>
  </body>
</html>
